package edu.ncsu.csc.csc454.project.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.ncsu.csc.csc454.project.gui.info_panel.InfoPanel;
import edu.ncsu.csc.csc454.project.gui.wheel_panel.WheelDisplay;
import edu.ncsu.csc.csc454.project.resources.Constants;

/**
 * Represents the application's graphical user interface. The primary MVC "View" class.
 * @author Team AW
 * @version 0.1
 */
@SuppressWarnings("serial")
public class ApplicationWheelGUI extends JFrame {

    /**
     * The contentPane for this container.
     */
    private JPanel       contentPane;

    /**
     * Location where the wheel will be rendered.
     */
    private WheelDisplay wheel;

    private InfoPanel    infoPanel;

    /**
     * Location where the menu buttons are placed.
     */
    private JPanel       menuPanel = new JPanel();

    /**
     * Just a dummy menu button.
     */
    private JButton      menuButton;

    public ApplicationWheelGUI() {
        setTitle("Application Wheel");
        setSize(Constants.GUI_SIZE);

        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());

        buildInfoPanel();
        buildMenuPanel();
        buildWheelPanel();
    }

    private void buildInfoPanel() {
        infoPanel = new InfoPanel();
        add(infoPanel, BorderLayout.NORTH);
    }

    /**
     * This will likely need to become another class just like infoPanel is...
     */
    private void buildMenuPanel() {
        menuPanel.setBackground(Color.BLACK);

        try {
            BufferedImage bim = ImageIO.read(new File(Constants.MENU_BUTTON_PATH));
            menuButton = new JButton(new ImageIcon(bim));
            menuButton.setPreferredSize(new Dimension(bim.getWidth(), bim.getHeight()));
        } catch (Exception e) {
            menuButton = new JButton("Menu");
        }

        menuPanel.add(menuButton);
        contentPane.add(menuPanel, BorderLayout.SOUTH);
    }

    private void buildWheelPanel() {
        wheel = new WheelDisplay();
        contentPane.add(wheel, BorderLayout.CENTER);
    }

    /**
     * Discovers the best position on-screen for the GUI's location, then displays the GUI there.
     */
    public void display() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (getSize().height > screenSize.height) getSize().height = screenSize.height;
        if (getSize().width > screenSize.width) getSize().width = screenSize.width;

        setLocation((screenSize.width - getSize().width) / 2,
                (screenSize.height - getSize().height) / 2);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        validate();
        wheel.initFrame();
    }

    public void refresh() {

    }

    public JButton getMenuButton() {
        return menuButton;
    }

    public WheelDisplay getWheel() {
        return wheel;
    }
}
